<?php

namespace app\api\controller;

use app\common\controller\Api;
use fast\AdapayUtil;
use think\Db;
use think\Log;
use fast\RsaUtil;

/**
 * 首页接口
 */
class Refund extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];




    public function index()
    {
        
        $post = $this->request->request();




        logs('huiyuan_refund',json_encode($post,JSON_UNESCAPED_UNICODE)); 


        $post['signContent'] = html_entity_decode($post['signContent']);



        logs('huiyuan_refund','signContent_______'.$post['signContent']); 


        $signContent = json_decode($post['signContent'],true);

        $signStr = "method=".$post['method']."&version=".$post['version']."&format=".$post['format']."&merchantNo=".$post['merchantNo']."&signType=".$post['signType']."&signContent=".$post['signContent']."&key=5cbb0128c4489a7b5b9a148fb342ecee";
        $destDir = ROOT_PATH . 'public'. DS;

        $rsaUtil = new RsaUtil($destDir .'dongbaoyun_pri.pfx',$destDir .'MANDAO_810082423610_pub.cer','123456');

        $verifyResult = $rsaUtil->verify($signStr,$post['sign']);
        

      

        if($verifyResult){
            if (isset($signContent['orderStatus']) &&  $signContent['orderStatus'] == 'SUCCESS') {
             
                $where['order_no'] = $signContent['origTransNo'];
                $info = Db::table('fa_pifa_orderliushui')->where($where)->find();
                if (!empty($info)) {
                        $updata1['re_status'] = '1';
                        Db::table('fa_pifa_orderliushui')->where($where)->update($updata1);

                        $where_order['id'] = $info['order_id'];

                        $updata['orderstate'] = 4;
                        $updata['shippingstate'] = 1;
                        $updata['sh_status'] = 2;
                        $updata['refundtime'] = time();

                        Db::table('fa_pifa_order')->where($where_order)->update($updata);
                 return 'SUCCESS';
                }
            }     
        }
        
        return 'FAIL';
        
    
      


     
        // logs('huiyuan_notify2',json_encode($post1,JSON_UNESCAPED_UNICODE));              
        
        
 

        
    }



}
